package com.itheima.controller;

import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "检查组模块管理")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    //   开发 检查项模块   crud   编辑模块也在这
    @PostMapping("checkgroup/add")
    @ApiOperation(value = "检查组添加",notes = "根据表单提交数据，录入到数据库")
    public Result add(@RequestBody CheckGroupDTO checkGroupDTO){
        try {
            checkGroupService.saveCheckGroup(checkGroupDTO);
            return  new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }


    //   开发 检查项模块   crud
    @PostMapping("checkgroup/findPage")
    @ApiOperation(value = "分页查询检查组方法", notes = "分页查询的检查组列表信息")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = checkGroupService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }


    //编辑修改
    @GetMapping("checkgroup/findCheckItemIdsByGroupId")
    @ApiOperation(value = "编辑修改检查组方法", notes = "编辑修改检查组列表信息")
    public Result update(@RequestParam("id")int id) {
        try {
            List<Integer> list = checkGroupService.findCheckItemIdsByGroupId(id);
            return  new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return   new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }

    //删除模块
    @DeleteMapping("checkgroup/delete")
    @ApiOperation(value = "删除检查组方法", notes = "删除检查组列表信息")
    public Result delete(@RequestParam("id") Integer id) {
        //先获取要删除的信息对象id  执行物理删除中间表以及Checkgroup表对应数据
        try {
            String s = checkGroupService.deleteyGroupById(id);
            if (StringUtils.isEmpty(s)){
                return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
            }else {
                return new Result(true,s);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    //   查询所有检查组
    @GetMapping("checkgroup/findAll")
    @ApiOperation(value = "查询检查组方法", notes = "查询所有的检查组列表信息")
    public Result findAll() {
        try {
            List<CheckGroup> list = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }


}
